package org.diffgenova;

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Filter;
import android.widget.Filter.FilterListener;
import android.widget.FilterQueryProvider;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class ListCategorieActivity extends ListActivity {
    Map<String, String> rifiuti = new HashMap<String, String>();
    private DataBaseHelper dbHelper;
    private SimpleCursorAdapter adapter;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_categorie);
        ListView lv;
        lv = getListView();
        lv.setTextFilterEnabled(true);
        
    }
    
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Cursor c = (Cursor) l.getItemAtPosition(position);
        String rifiuto = c.getString(0);
        String categoria = c.getString(1);
        Log.e("rifiuto", "Rifiuto: " + rifiuto);
        Log.e("rifiuto", "Categoria: " + categoria);
//        Toast.makeText(getApplicationContext(), categoria, Toast.LENGTH_SHORT)
//                .show();
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra("categoria", categoria);
        startActivity(intent);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        dbHelper = new DataBaseHelper(getApplicationContext());
        try {
            dbHelper.createDataBase();
            dbHelper.openDataBase();
            Cursor contenitori = dbHelper.getCategorie();
            String[] columns = {
                    "categoriades"
            }; // , "categoriades" };
            int[] textIds = {
                    R.id.rifiuto
            }; // , R.layout.list_item };
            adapter = new ListCategorieAdapter(getApplicationContext(),
                    R.layout.list_item, contenitori, columns, textIds);
            adapter.setFilterQueryProvider(new TextQuery());
            setListAdapter(adapter);
        } catch (IOException e) {
            Toast.makeText(getApplicationContext(), e.getClass().getName()
                    + ": " + e.getMessage(), Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
    
    @Override
    protected void onPause() {
        dbHelper.close();
        super.onPause();
    }
    
    class TextQuery implements FilterQueryProvider {
        public Cursor runQuery(CharSequence arg0) {
            // .... build a new select and provide a cursor
            // dbHelper.openDataBase();
            Cursor rifiuti = dbHelper.getRifiuti(arg0.toString());
            // dbHelper.close();
            return rifiuti;
        }
    }
    
}
